package com.example.myapplication;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SwitchCompat;

import java.util.List;

/**
 * 禁用话题列表Activity
 * 显示所有禁用话题
 */
public class BlockedTopicListActivity extends AppCompatActivity {

    private TopicRepository topicRepository;
    private LinearLayout containerTopics;
    private TextView btnAddTopic;
    private ImageView btnBack;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_blocked_topic_list);

        topicRepository = new TopicRepository(this);
        
        initViews();
        setupListeners();
    }

    @Override
    protected void onResume() {
        super.onResume();
        loadTopics();
    }

    private void initViews() {
        containerTopics = findViewById(R.id.container_topics);
        btnAddTopic = findViewById(R.id.btn_add_topic);
        btnBack = findViewById(R.id.btn_back);
    }

    private void setupListeners() {
        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        btnAddTopic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(BlockedTopicListActivity.this, AddBlockedTopicActivity.class);
                startActivity(intent);
            }
        });
    }

    private void loadTopics() {
        containerTopics.removeAllViews();
        
        List<BlockedTopic> topics = topicRepository.getAllTopics();
        
        if (topics.isEmpty()) {
            // 显示空状态
            TextView emptyView = new TextView(this);
            emptyView.setText("暂无禁用话题");
            emptyView.setTextSize(14);
            emptyView.setTextColor(getResources().getColor(android.R.color.darker_gray));
            emptyView.setPadding(32, 32, 32, 32);
            emptyView.setGravity(android.view.Gravity.CENTER);
            containerTopics.addView(emptyView);
            return;
        }
        
        for (final BlockedTopic topic : topics) {
            View itemView = LayoutInflater.from(this).inflate(R.layout.item_blocked_topic, containerTopics, false);
            
            TextView textTopic = itemView.findViewById(R.id.text_topic);
            SwitchCompat switchEnabled = itemView.findViewById(R.id.switch_enabled);
            
            textTopic.setText(topic.getTopic());
            switchEnabled.setChecked(topic.isEnabled());
            
            // 开关监听
            switchEnabled.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    topic.setEnabled(isChecked);
                    topicRepository.updateTopic(topic);
                }
            });
            
            // 点击编辑
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(BlockedTopicListActivity.this, AddBlockedTopicActivity.class);
                    intent.putExtra(AddBlockedTopicActivity.EXTRA_TOPIC_ID, topic.getId());
                    startActivity(intent);
                }
            });
            
            // 长按删除
            itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    showDeleteConfirmDialog(topic);
                    return true;
                }
            });
            
            containerTopics.addView(itemView);
        }
    }

    private void showDeleteConfirmDialog(final BlockedTopic topic) {
        new android.app.AlertDialog.Builder(this)
            .setTitle("删除话题")
            .setMessage("确定要删除话题 \"" + topic.getTopic() + "\" 吗？")
            .setPositiveButton("删除", new android.content.DialogInterface.OnClickListener() {
                @Override
                public void onClick(android.content.DialogInterface dialog, int which) {
                    topicRepository.deleteTopic(topic.getId());
                    loadTopics();
                    Toast.makeText(BlockedTopicListActivity.this, "已删除", Toast.LENGTH_SHORT).show();
                }
            })
            .setNegativeButton("取消", null)
            .show();
    }
}

